<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/pages/common/header.jsp"%>
</head>
<body>
	<div class="contentBody contentborder">
		<div class="tab-pane" id="tabPane">
			<div id="message">${message }</div>
			<div class="tab-page">
				 <h1 class="tab">装箱单管理</h1>
 				 <div class="contentborder">
                	 <jdf:form bean="request" scope="request">
						<form action="${dynamicDomain}/packing/${action}" method="post">
							 <table class="inputTable">
			                 	<tr>
									<td class="label cancelBorderLeft">
                                        <label for="search_EQI_customerId">客户：</label>
                                    </td>
                                    <td class="content" colspan="3">
                                        <select name="search_EQI_customerId" style="width:400px;" id="search_EQI_customerId">
                                            <option value=""></option>
                                            <jdf:selectCollection items="customers" optionValue="objectId" optionText="name"/>
                                        </select>
                                    </td>
			                 	</tr>
			                 	<tr>
			                 	    <td class="label cancelBorderLeft">
                                        <label for="search_EQI_objectId">装箱单编号：</label>
                                    </td>
                                    <td class="content">
                                        <input type="text" name="search_EQI_objectId" />
                                    </td>
                                    <td class="label">
                                        <label for="search_GED_eta">应付日期：</label>
                                    </td>
                                    <td class="content">
                                       <input type="text" id="search_GED_eta" size="12" name="search_GED_eta" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'#F{$dp.$D(\'search_LED_eta\')}',readOnly:true})">
                                        ~
                                        <input type="text" id="search_LED_eta" size="12" name="search_LED_eta" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy-MM-dd',minDate:'#F{$dp.$D(\'search_GED_eta\')}',readOnly:true})">
                                    </td>
                                </tr>
			                 	<tr>
                                	<td class="bottomLabel" colspan="4">
                                		<div class="right">
											<button type="reset">重置</button>
											<button type="submit">查询</button>
                                		</div>
									</td>
			                 </table>
			            </form>
			        </jdf:form>
				</div>
				<div class="contentborder listArea">
					<div class="toolBar">
		 				<button type="button" onclick="window.location.href='${dynamicDomain}/packing/create?action=${action}'">增加</button>
					</div>
					<div id="tableDiv">
						<jdf:table items="items" var="currentRowObject" retrieveRowsCallback="limit" filterRowsCallback="limit" sortRowsCallback="limit" action="${action}">
							<jdf:export view="csv" fileName="订单.csv" tooltip="Export CSV" imageName="csv" />
							<jdf:export view="xls" fileName="订单.xls" tooltip="Export EXCEL" imageName="xls" />
							<jdf:row>
								<jdf:column property="objectId" title="装箱单编号" style="width:15%">
								    ${currentRowObject.no}
								</jdf:column>
								<jdf:column property="customerId" title="客户" style="width: 25%">
                                  <jdf:columnCollectionValue items="customers"  nameProperty="name" value="${currentRowObject.customerId}"/>
                                </jdf:column>
                                <jdf:column property="eta" title="ETA" style="width: 15%" cell="date"/>
								<jdf:column property="commercialInvoiceId" title="发票编号" style="width: 15%">
								    ${currentRowObject.commercialInvoiceNo}
								</jdf:column>
								<jdf:column property="status" title="状态" style="width: 10%">
									<jdf:columnValue dictionaryId="1001" value="${currentRowObject.status}"/>
								</jdf:column>
								<jdf:column alias="操作" sortable="false" viewsAllowed="html" style="width: 20%">
									<c:if test="${action=='page' }">
									<a href="${dynamicDomain}/packing/edit/${currentRowObject.id}?action=${action}" class="button">修改</a>
									<a href="${dynamicDomain}/packing/print/${currentRowObject.objectId}?action=${action}" class="button">查看</a>
									</c:if>
									<c:if test="${action=='audit' }">
										<a href="javascript:audit(${currentRowObject.objectId})" class="button  audit">审核通过</a>
									</c:if>
									<a href="#selectUser" onclick="javascript:currentOrderId=${currentRowObject.objectId};getCustomer(${currentRowObject.customerId});" class="button audit edit">邮件通知</a>
								    <a href="${dynamicDomain}/packing/download/${currentRowObject.objectId}" class="button">PDF下载</a>
								</jdf:column>
							</jdf:row>
						</jdf:table>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div style="display: none;">
        <div id="selectUser" class="contentborder">
            <div id="suberror"></div>
            <div id="subnotice"></div>
            <table class="inputTable">
               <tr>
                   <td class="label cancelBorderLeft" style="width: 20%;"><label>客户：</label></td>
                   <td class="content">&nbsp;<input type="checkbox" name="customer" value="1" id="customer_1" style="vertical-align: middle;">&nbsp;<label id="customer_2" for="customer_1"></label></td>
               </tr>
               <jsp:include page="/extendUser/selectEmailUsers" flush="true" />
               <tr>
                   <td class="bottomLabel" colspan="2">
                      <div class="right">
                          <button onclick="notify()" id="notifyButton">邮件通知</button>
                      </div>
                  </td>
               </tr>
            </table>
        </div>
    </div>
	<script type="text/javascript">
	
		$(function() {
			$('.edit').colorbox({ inline:true,innerWidth:500, innerHeight:300,href:'#selectUser'});
	    });
	
		function audit(orderId){
			$(".audit").attr("disabled",true);
			$.ajax({
		         url:"${dynamicDomain}/packing/audit/" + orderId,
		         type:'post',
		         dataType:'json',
		         data:"status=20" ,
		         success:function(msg){
		        	 if(msg.result){
		        		 window.location.href='<%=dynamicDomain%>/packing/${action}?message=' + encodeURIComponent('操作成功');
		        	 }else{
		        		 showMessage("操作失败");
		        		 $(".audit").attr("disabled",false);
		        	 }
		         }
			});
		}
	    
		var currentOrderId = null;
        
        function getCustomer(id){
            $("#suberror").hide();
            $("#subnotice").hide();
            $("#otherUsers").val("");
            $("input[type='checkbox']").each(function(){
                $(this).attr("checked",false);
            });
            if(id){
                $.ajax({
                     url:"${dynamicDomain}/customer/get/" + id,
                     type:'post',
                     dataType:'json',
                     success:function(msg){
                         $("#customer_2").text(msg.entity.name).attr("title",msg.entity.email);
                     }
                });
            }
        }
        
        function notify(){
            if(currentOrderId==null){
                return;
            }
            $(".audit").attr("disabled",true);
            $("#notifyButton").attr("disabled",true);
            var selected = getCheckedValuesString(document.getElementsByName("users"));
            var customer = ($("#customer_1").attr("checked")=="checked");
            var others = $("#otherUsers").val();
            if(!selected&&!customer&&!others){
                $("#suberror").html("请至少选择一个接收者").show();
                $(".audit").attr("disabled",false);
                $("#notifyButton").attr("disabled",false);
            }else{
                $("#suberror").hide();
                $("#subnotice").html("操作进行中...").show();
                $.ajax({
                     url:"${dynamicDomain}/packing/notify/" + currentOrderId + "?date=" + (new Date()).valueOf(),
                     type:'post',
                     dataType:'json',
                     data:'purchasers=' + (selected==null?"":selected) + "&customer=" + customer + "&others=" + others,
                     success:function(msg){
                         if(msg.result){
                             showMessage("邮件已发送");
                         }else{
                             showMessage("操作失败");
                         }
                         $(".audit").attr("disabled",false);
                         $("#notifyButton").attr("disabled",false);
                         jQuery.fn.colorbox.close();
                     }
                });
            }
            
        }
		
	</script>
</body>
</html>